home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 November: Tool Chest / Dev.CD Nov 00 TC Disk 2.toast / pc / sample code / processes / mp threaded sort / shellsort.cp < prev    next >
Encoding:
Text File  |  2000-09-28  |  1.8 KB  |  71 lines

  1. /*
  2.     File:        ShellSort.cp
  3.  
  4.     Contains:    Apply a ShellSort algorithm to an offscreen, scrambled GWorld
  5.  
  6.     Written by:     
  7.  
  8.     Copyright:    Copyright © 1988-1999 by Apple Computer, Inc., All Rights Reserved.
  9.  
  10.                 You may incorporate this Apple sample source code into your program(s) without
  11.                 restriction. This Apple sample source code has been provided "AS IS" and the
  12.                 responsibility for its operation is yours. You are not permitted to redistribute
  13.                 this Apple sample source code as "Apple sample source code" after having made
  14.                 changes. If you're going to re-distribute the source, we require that you make
  15.                 it clear in the source that the code was descended from Apple sample source
  16.                 code, but that you've made changes.
  17.  
  18.     Change History (most recent first):
  19.                 7/27/1999    Karl Groethe    Updated for Metrowerks Codewarror Pro 2.1
  20.                 
  21.  
  22. */
  23. #include "SortPicts.h"
  24.  
  25. void    ShellSort( SortPicts *sortPicts)
  26. {
  27.     sortPicts->ShellSort();
  28. }
  29.  
  30. /*************************************************************/
  31. /*                                                           */
  32. /*               The Actual Sort Algorithms                  */
  33. /*                                                           */
  34. /*************************************************************/
  35.  
  36.  
  37. void    SortPicts :: ShellSort( void)
  38. {
  39.     long            loop, sortSize, inLoop;
  40.     long            min;
  41.     long            data;
  42.  
  43.     sortSize = 1;
  44.     while( sortSize < N)
  45.         sortSize = 3 * sortSize + 1;
  46.     
  47.     do
  48.     {
  49.         sortSize /= 3;
  50.         for( loop = sortSize; loop < N; ++loop)
  51.         {
  52.             min = sortData[loop];
  53.  
  54.             for( inLoop = loop; (data = sortData[inLoop - sortSize]) > min; )
  55.             {
  56.                 SetSortItem( inLoop, data);
  57.  
  58.                 inLoop -= sortSize;
  59.                 if( inLoop < sortSize)
  60.                     break;
  61.             }
  62.             
  63.         SetSortItem( inLoop, min);
  64.         }    
  65.                 
  66.     } while( sortSize > 0);            //  1 = cool picts; 0 = actual sort
  67. }
  68.  
  69.  
  70.  
  71.